#include "town.h"
#include "game_engine.h"

#include "building/bldg_ctzn_spawner.h"

town::town(): lastbuilding( clock() ){}

void town::execAI(clock_t now){

    unsigned int countbuildings = 0;
    game_engine::mu_lock(mtx_buildlock);{
        blist_t* builds = game_engine::getBuilds();
        for( blist_t::iterator it = builds->begin(); it != builds->end(); it++ ){
            countbuildings++;
        }
    game_engine::mu_unlock(mtx_buildlock);}

    if( (now - lastbuilding) > (1000*countbuildings) && countbuildings < 10){
        building* bl = new bldg_ctzn_spawner(0,0);
        do{
            bl->pos.x = D( game_engine::getMap()->getWidth() )-1;
            bl->pos.y = D( game_engine::getMap()->getHeight() )-1;
        }while( !bl->hasSpace(game_engine::getMap()) );
        game_engine::mu_lock(mtx_buildlock);{
            game_engine::addBuild(bl);
        game_engine::mu_unlock(mtx_buildlock);}
        lastbuilding = now;
    }

}

